/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PiutangFrame.java
 *
 * Created on Jun 25, 2013, 11:28:53 AM
 */
package com.ivanbiz.gui;

import com.ivanbiz.dao.MemberCustomerDAO;
import com.ivanbiz.dao.PiutangDAO;
import com.ivanbiz.dao.PiutangHistoryDAO;
import com.ivanbiz.dao.PenjualanDAO;
import com.ivanbiz.dao.impl.MemberCustomerDAOImpl;
import com.ivanbiz.dao.impl.PiutangDAOImpl;
import com.ivanbiz.dao.impl.PiutangHistoryDAOImpl;
import com.ivanbiz.dao.impl.PenjualanDAOImpl;
import com.ivanbiz.helper.GlobalConstant;
import com.ivanbiz.helper.GlobalSession;
import com.ivanbiz.helper.ServiceHelper;
import com.ivanbiz.helper.UserSession;
import com.ivanbiz.model.MemberCustomer;
import com.ivanbiz.model.Piutang;
import com.ivanbiz.model.PiutangHistory;
import com.ivanbiz.model.Penjualan;
import com.ivanbiz.model.User;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author IW20149X
 */
public class PiutangFrame extends javax.swing.JInternalFrame {

    List<Piutang> listPiutang = new ArrayList<Piutang>();
    List<PiutangHistory> listPiutangHistory = new ArrayList<PiutangHistory>();
    List<Penjualan> listPenjualan = new ArrayList<Penjualan>();
    MemberCustomerDAO memberCustomerDAO = new MemberCustomerDAOImpl();
    PiutangDAO piutangDAO = new PiutangDAOImpl();
    PiutangHistoryDAO piutangHistoryDAO = new PiutangHistoryDAOImpl();
    PenjualanDAO penjualanDAO = new PenjualanDAOImpl();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MMMM yyyy");
    NumberFormat numberFormat = NumberFormat.getInstance(Locale.US);
    double sisaPiutang = 0;
    ServiceHelper serviceHelper = new ServiceHelper();
    User user = UserSession.getUser();

    /** Creates new form PiutangFrame */
    public PiutangFrame() {
        initComponents();
        updateData();
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dialogPOI = new javax.swing.JDialog();
        jPanel10 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        tableData = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        buttonJatuhTempo = new javax.swing.JButton();
        buttonBelumLunas = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        textSearch = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablePiutang = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        labelPO = new javax.swing.JLabel();
        textPO = new javax.swing.JTextField();
        labelPelanggan = new javax.swing.JLabel();
        textPelanggan = new javax.swing.JTextField();
        labelSisaHutang = new javax.swing.JLabel();
        textSisaHutang = new javax.swing.JTextField();
        labelJatuhTempo = new javax.swing.JLabel();
        textJatuhTempo = new javax.swing.JTextField();
        buttonPO = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tablePiutangHistory = new javax.swing.JTable();
        buttonBayar = new javax.swing.JButton();
        textBayar = new javax.swing.JTextField();
        buttonPrint = new javax.swing.JButton();

        dialogPOI.setModal(true);

        jPanel10.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Table Data", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Monospaced", 0, 11))); // NOI18N

        tableData.setAutoCreateRowSorter(true);
        tableData.setFont(new java.awt.Font("Monospaced", 1, 14));
        tableData.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tableData.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableDataMouseClicked(evt);
            }
        });
        jScrollPane4.setViewportView(tableData);

        javax.swing.GroupLayout jPanel10Layout = new javax.swing.GroupLayout(jPanel10);
        jPanel10.setLayout(jPanel10Layout);
        jPanel10Layout.setHorizontalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel10Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 466, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel10Layout.setVerticalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel10Layout.createSequentialGroup()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout dialogPOILayout = new javax.swing.GroupLayout(dialogPOI.getContentPane());
        dialogPOI.getContentPane().setLayout(dialogPOILayout);
        dialogPOILayout.setHorizontalGroup(
            dialogPOILayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(dialogPOILayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        dialogPOILayout.setVerticalGroup(
            dialogPOILayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(dialogPOILayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("table Hutang"));

        buttonJatuhTempo.setFont(new java.awt.Font("Monospaced", 1, 14)); // NOI18N
        buttonJatuhTempo.setText("Jatuh Tempo");
        buttonJatuhTempo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonJatuhTempoActionPerformed(evt);
            }
        });

        buttonBelumLunas.setFont(new java.awt.Font("Monospaced", 1, 14)); // NOI18N
        buttonBelumLunas.setText("Belum Lunas");
        buttonBelumLunas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBelumLunasActionPerformed(evt);
            }
        });

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Search", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Monospaced", 0, 11))); // NOI18N

        textSearch.setFont(new java.awt.Font("Monospaced", 1, 14));
        textSearch.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textSearchKeyReleased(evt);
            }
        });

        tablePiutang.setAutoCreateRowSorter(true);
        tablePiutang.setFont(new java.awt.Font("Monospaced", 1, 14)); // NOI18N
        tablePiutang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tablePiutang.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablePiutangMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tablePiutang);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
                    .addComponent(textSearch, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(textSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 332, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(buttonJatuhTempo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonBelumLunas)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonBelumLunas)
                    .addComponent(buttonJatuhTempo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Hutang"));

        labelPO.setFont(new java.awt.Font("Monospaced", 1, 14));
        labelPO.setText("NO PO");

        textPO.setEditable(false);
        textPO.setFont(new java.awt.Font("Monospaced", 1, 14));
        textPO.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                textPOMouseClicked(evt);
            }
        });

        labelPelanggan.setFont(new java.awt.Font("Monospaced", 1, 14));
        labelPelanggan.setText("Pelanggan");

        textPelanggan.setEditable(false);
        textPelanggan.setFont(new java.awt.Font("Monospaced", 1, 14));

        labelSisaHutang.setFont(new java.awt.Font("Monospaced", 1, 14));
        labelSisaHutang.setText("Sisa Hutang");

        textSisaHutang.setEditable(false);
        textSisaHutang.setFont(new java.awt.Font("Monospaced", 1, 14));

        labelJatuhTempo.setFont(new java.awt.Font("Monospaced", 1, 14));
        labelJatuhTempo.setText("Jatuh Tempo");

        textJatuhTempo.setEditable(false);
        textJatuhTempo.setFont(new java.awt.Font("Monospaced", 1, 14));

        buttonPO.setText("...");
        buttonPO.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonPOActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelSisaHutang)
                    .addComponent(labelPelanggan)
                    .addComponent(labelPO)
                    .addComponent(labelJatuhTempo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textJatuhTempo, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE)
                    .addComponent(textPelanggan, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE)
                    .addComponent(textSisaHutang, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(textPO, javax.swing.GroupLayout.DEFAULT_SIZE, 443, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonPO, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(textPO, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(labelPO))
                    .addComponent(buttonPO))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textPelanggan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelPelanggan))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textSisaHutang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelSisaHutang))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textJatuhTempo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelJatuhTempo))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("table Hutang History"));

        tablePiutangHistory.setAutoCreateRowSorter(true);
        tablePiutangHistory.setFont(new java.awt.Font("Monospaced", 1, 14));
        tablePiutangHistory.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tablePiutangHistory);

        buttonBayar.setFont(new java.awt.Font("Monospaced", 1, 14));
        buttonBayar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/gui/icon/Aprove.jpg"))); // NOI18N
        buttonBayar.setText(" Bayar");
        buttonBayar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBayarActionPerformed(evt);
            }
        });

        textBayar.setFont(new java.awt.Font("Monospaced", 1, 14));

        buttonPrint.setFont(new java.awt.Font("Monospaced", 1, 14));
        buttonPrint.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ivanbiz/gui/icon/Print.jpg"))); // NOI18N
        buttonPrint.setText("Print");
        buttonPrint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonPrintActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 565, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(textBayar, javax.swing.GroupLayout.DEFAULT_SIZE, 335, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonBayar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonPrint)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textBayar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonPrint)
                    .addComponent(buttonBayar))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tablePiutangMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablePiutangMouseClicked
        try {
            buttonBayar.setEnabled(true);
            buttonPrint.setEnabled(true);
            Piutang piutang = (Piutang) listPiutang.get(tablePiutang.getSelectedRow());
            textPO.setText(piutang.getNoTransaksi());
            MemberCustomer memberCustomer = memberCustomerDAO.getCustomer(piutang.getNoTransaksi());
            textPelanggan.setText(memberCustomer.getKodeMember() + "(" + memberCustomer.getNama() + ")");
            textJatuhTempo.setText("" + simpleDateFormat.format(piutang.getJatuhTempo()));
            sisaPiutang = piutang.getSisaHutang();
            textSisaHutang.setText("Rp " + numberFormat.format(piutang.getSisaHutang()) + ",00");
            listPiutangHistory = piutangDAO.getDataByEquals(PiutangHistory.class, "hutang.id", piutang.getId());
            updateTablePiutangHistory();
        } catch (Exception ex) {
            Logger.getLogger(PiutangFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_tablePiutangMouseClicked

    private void buttonJatuhTempoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonJatuhTempoActionPerformed
        try {
            updateData();
            listPiutang = piutangDAO.getByJatuhTempo(serviceHelper.getDateNow());
            updateTablePiutang();
        } catch (Exception ex) {
            Logger.getLogger(PiutangFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_buttonJatuhTempoActionPerformed

    private void buttonBelumLunasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBelumLunasActionPerformed
        try {
            updateData();
            listPiutang = piutangDAO.getByBelumLunas();
            updateTablePiutang();
        } catch (Exception ex) {
            Logger.getLogger(PiutangFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_buttonBelumLunasActionPerformed

    private void textPOMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_textPOMouseClicked
}//GEN-LAST:event_textPOMouseClicked

    private void buttonPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonPrintActionPerformed
}//GEN-LAST:event_buttonPrintActionPerformed

    private void buttonBayarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBayarActionPerformed
        Piutang piutang = (Piutang) listPiutang.get(tablePiutang.getSelectedRow());
        if (Double.parseDouble(textBayar.getText()) > sisaPiutang || Double.parseDouble(textBayar.getText()) <= 0) {
            JOptionPane.showMessageDialog(null, "Jumlah Bayar Melebihi Sisa Piutang !!!");
        } else {
            int response = JOptionPane.showConfirmDialog(null, GlobalConstant.PROCCESS, GlobalConstant.CONFIRM, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (response == JOptionPane.YES_OPTION) {
                try {
                    PiutangHistory piutangHistory = new PiutangHistory();
                    piutangHistory.setHutang(piutang);
                    piutangHistory.setPiutang(piutang.getSisaHutang());
                    piutangHistory.setBayar(Double.parseDouble(textBayar.getText()));
                    piutangHistory.setSisa(piutang.getSisaHutang() - Double.parseDouble(textBayar.getText()));
                    piutangHistory.setCreatedDate(new Date());
                    piutangHistory.setCreatedBy(user.getUserName());
                    piutang.setSisaHutang(piutang.getSisaHutang() - Double.parseDouble(textBayar.getText()));
                    piutangHistoryDAO.save(piutangHistory, piutang);
                    sisaPiutang = 0;
                    listPiutangHistory = piutangHistoryDAO.getDataByEquals(PiutangHistory.class, "hutang.id", piutangHistory.getHutang().getId());
                    updateTablePiutangHistory();
                    textBayar.setText(GlobalConstant.EMPTY_STRING);
                } catch (Exception ex) {
                    Logger.getLogger(PiutangFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
}//GEN-LAST:event_buttonBayarActionPerformed

    private void textSearchKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textSearchKeyReleased
}//GEN-LAST:event_textSearchKeyReleased

    private void buttonPOActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonPOActionPerformed
        try {
            listPenjualan = penjualanDAO.getDataByEquals(Penjualan.class, "noTransaksi", textPO.getText());
            updateTablePenjualan();
            dialogPOI.setSize(GlobalSession.getDesktopsize());
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            Dimension frameSize = dialogPOI.getSize();
            dialogPOI.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
            dialogPOI.setVisible(true);
        } catch (Exception ex) {
            Logger.getLogger(PiutangFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_buttonPOActionPerformed

    private void tableDataMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableDataMouseClicked
}//GEN-LAST:event_tableDataMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonBayar;
    private javax.swing.JButton buttonBelumLunas;
    private javax.swing.JButton buttonJatuhTempo;
    private javax.swing.JButton buttonPO;
    private javax.swing.JButton buttonPrint;
    private javax.swing.JDialog dialogPOI;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JLabel labelJatuhTempo;
    private javax.swing.JLabel labelPO;
    private javax.swing.JLabel labelPelanggan;
    private javax.swing.JLabel labelSisaHutang;
    private javax.swing.JTable tableData;
    private javax.swing.JTable tablePiutang;
    private javax.swing.JTable tablePiutangHistory;
    private javax.swing.JTextField textBayar;
    private javax.swing.JTextField textJatuhTempo;
    private javax.swing.JTextField textPO;
    private javax.swing.JTextField textPelanggan;
    private javax.swing.JTextField textSearch;
    private javax.swing.JTextField textSisaHutang;
    // End of variables declaration//GEN-END:variables

    private void updateData() {
        clearData();
        listPiutang.clear();
        listPiutangHistory.clear();
        updateTablePiutang();
        updateTablePiutangHistory();
    }

    private void clearData() {
        textSearch.setVisible(false);
        textSearch.setText(GlobalConstant.EMPTY_STRING);
        textPO.setText(GlobalConstant.EMPTY_STRING);
        textPelanggan.setText(GlobalConstant.EMPTY_STRING);
        textSisaHutang.setText(GlobalConstant.EMPTY_STRING);
        textJatuhTempo.setText(GlobalConstant.EMPTY_STRING);
        textBayar.setText(GlobalConstant.EMPTY_STRING);
        buttonPrint.setVisible(false);
    }

    private void updateTablePiutang() {
        String[] judul = {GlobalConstant.NO, labelPelanggan.getText(), GlobalConstant.NAMA, labelPO.getText(), labelJatuhTempo.getText()};
        Object[][] isi = new Object[listPiutang.size()][5];
        int x = 0;
        int no = 0;
        for (Piutang piutang : listPiutang) {
            try {
                MemberCustomer memberCustomer = memberCustomerDAO.getCustomer(piutang.getNoTransaksi());
                piutang.setMemberCustomer(memberCustomer);
                no += 1;
                isi[x][0] = no;
                isi[x][1] = piutang.getMemberCustomer().getKodeMember();
                isi[x][2] = piutang.getMemberCustomer().getKodeMember();
                isi[x][3] = piutang.getNoTransaksi();
                isi[x][4] = simpleDateFormat.format(piutang.getJatuhTempo());
                x++;
            } catch (Exception ex) {
                Logger.getLogger(PiutangFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        tablePiutang.setModel(new DefaultTableModel(isi, judul) {

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return false;
            }
        });
        TableColumn column0 = tablePiutang.getColumnModel().getColumn(0);
        column0.setPreferredWidth(50);
        column0.setMaxWidth(50);
    }

    private void updateTablePiutangHistory() {
        String[] judul = {"Tanggal Bayar", "Piutang", "Sudah Bayar", "Sisa Piutang"};
        Object[][] isi = new Object[listPiutangHistory.size()][4];
        int x = 0;
        int no = 0;
        for (PiutangHistory piutangHistory : listPiutangHistory) {
            no += 1;
            isi[x][0] = simpleDateFormat.format(piutangHistory.getCreatedDate());
            isi[x][1] = "Rp " + numberFormat.format(piutangHistory.getPiutang()) + ",00";
            isi[x][2] = "Rp " + numberFormat.format(piutangHistory.getBayar()) + ",00";
            isi[x][3] = "Rp " + numberFormat.format(piutangHistory.getSisa()) + ",00";
            x++;
        }
        tablePiutangHistory.setModel(new DefaultTableModel(isi, judul) {

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return false;
            }
        });
    }

    private void updateTablePenjualan() {
        Object[] judul = {GlobalConstant.NO, GlobalConstant.KODE_BARCODE, GlobalConstant.NAMA, GlobalConstant.QUANTITY, GlobalConstant.HARGA_JUAL, GlobalConstant.DISKON, GlobalConstant.TOTAL};
        Object[][] isi = new Object[listPenjualan.size()][7];
        int x = 0;
        int no = 0;
        double total = 0;

        for (Penjualan penjualan : listPenjualan) {
            isi[x][0] = no += 1;
            isi[x][1] = penjualan.getItem().getKodeBarcode();
            isi[x][2] = penjualan.getItem().getNamaItem();
            isi[x][3] = penjualan.getQuantity();
            isi[x][4] = numberFormat.format(penjualan.getHargaJual());
            isi[x][5] = penjualan.getDiskon();
            isi[x][6] = numberFormat.format(penjualan.getHargaJual() * penjualan.getQuantity());
            x++;
        }
        tableData.setModel(new DefaultTableModel(isi, judul) {

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                if (columnIdentifiers.get(columnIndex).equals(GlobalConstant.DISKON) || columnIdentifiers.get(columnIndex).equals(GlobalConstant.QUANTITY)) {
                    return true;
                } else {
                    return false;
                }
            }
        });

        TableColumn column0 = tableData.getColumnModel().getColumn(0);
        column0.setPreferredWidth(75);
        column0.setMaxWidth(75);
        TableColumn column1 = tableData.getColumnModel().getColumn(1);
        column1.setPreferredWidth(130);
        column1.setMaxWidth(130);
        TableColumn column3 = tableData.getColumnModel().getColumn(3);
        column3.setPreferredWidth(75);
        column3.setMaxWidth(75);
        TableColumn column4 = tableData.getColumnModel().getColumn(4);
        column4.setPreferredWidth(150);
        column4.setMaxWidth(150);
        TableColumn column5 = tableData.getColumnModel().getColumn(5);
        column5.setPreferredWidth(75);
        column5.setMaxWidth(75);
        TableColumn column6 = tableData.getColumnModel().getColumn(6);
        column6.setPreferredWidth(150);
        column6.setMaxWidth(150);
    }
}
